import request from '@/config/axios'
import { ProcessDefinitionVO } from '@/api/bpm/model'
import { CandidateStrategy, NodeType } from '@/components/bpmnProcessDesigner/src/consts'
import { ProcessDefinitionInfoVO } from '@/api/bpm/process_definition'

export type Task = {
  id: string
  name: string
}

export type Activity = {
  id: string
  name: string
  type: string
  assigneeUser?: User
  instanceCount: number
}

export interface ProcessInstanceVO {
  id: string
  name: string
  startTime: string
  endTime: string
  durationInMillis: number
  businessKey: string
  processDefinitionId: string
  category: string
  status: number
  categoryName?: string
  formVariables?: any
  startFormVariables?: any
  processDefinition?: ProcessDefinitionVO
  startUser?: User
  tasks: Task[]
  activities: Activity[]
  taskActivities: Activity[]  // 客户端添加
}

export type ApprovalDetail = {
  status: number // 流程实例状态，可能是未开始状态 (流程开启场景)
  activityNodes: ApprovalNodeInfo[]
  formFieldsPermission: any // 当前步骤活动节点表单权限 (可能是指定活动的活动节点，也可能是todoTask对应的活动节点)
  todoTask?: any // 当前待办任务详情
  processDefinition: ProcessDefinitionInfoVO // 流程定义详情
  processInstance?: ProcessInstanceVO //流程实例详情
}

// 审批节点信息
export type ApprovalNodeInfo = {
  id: string
  name: string
  nodeType: NodeType
  candidateStrategy?: CandidateStrategy
  status: number
  startTime?: Date
  endTime?: Date
  candidateUsers?: User[]
  candidateGroups?: Group[]
  tasks: ApprovalTaskInfo[]
}

// 审批任务信息
export type ApprovalTaskInfo = {
  id: string
  ownerUser: User
  assigneeUser: User
  status: number
  reason: string
  signPicUrl: string
}

// 用户信息
export type User = {
  staffId: string
  nickname: string
  avatar: string
}

// 用户组信息
export type Group = {
  groupId: number
  groupType: string
  groupName: string
}

export const ProcessInstanceApi = {
  // 获取BPM流程实例详情
  getProcessInstance: async (id: string) => {
    return await request.get({ url: '/bpm/process-instance/get', params: { id } })
  },

  // 获取我的BPM流程实例分页
  getProcessInstanceMyPage: async (params: PageParam) => {
    // 参数包含流程变量，较复杂，且可能很长，所以采用post方式
    return await request.post({
      url: '/bpm/process-instance/my-page',
      data: params,
      extension: {
        ignoreRepeatCheck: true
      }
    })
  },

  // 获取BPM流程实例分页
  getProcessInstanceManagePage: async (params: PageParam) => {
    // 参数包含流程变量，较复杂，且可能很长，所以采用post方式
    return await request.post({
      url: '/bpm/process-instance/manage-page',
      data: params,
      extension: {
        ignoreRepeatCheck: true
      }
    })
  },

  // 获取BPM流程抄送分页
  getProcessInstanceCopyPage: async (params: PageParam) => {
    return await request.get({ url: '/bpm/process-instance/copy/page', params })
  },

  getApprovalDetail: async (data: any) => {
    // 参数包含流程变量，较复杂，且可能很长，所以采用post方式
    return await request.post({
      url: '/bpm/process-instance/get-approval-detail',
      data,
      extension: {
        ignoreRepeatCheck: true
      }
    })
  },

  getNextApprovalNodes: async (data: any) => {
    // 参数包含流程变量，较复杂，且可能很长，所以采用post方式
    return await request.post({
      url: '/bpm/process-instance/get-next-approval-nodes',
      data,
      extension: {
        ignoreRepeatCheck: true
      }
    })
  },

  createProcessInstance: async (data: any) => {
    return await request.post({ url: '/bpm/process-instance/create', data })
  },

  cancelProcessInstanceByStartUser: async (id: string, reason: string) => {
    return await request.post({
      url: '/bpm/process-instance/cancel-by-start-user',
      data: { id, reason }
    })
  },

  cancelProcessInstanceByAdmin: async (id: string, reason: string) => {
    return await request.post({
      url: '/bpm/process-instance/cancel-by-admin',
      data: { id, reason }
    })
  },

  getProcessInstanceBpmnModelView: async (id: string) => {
    return await request.get({ url: '/bpm/process-instance/get-bpmn-model-view', params: { id } })
  }
}
